package com.study.leetcode;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Solution_648 {
    public static String replaceWords(List<String> dictionary, String sentence) {
        return Arrays.stream(sentence.split(" "))
                .map(
                        sen -> dictionary.stream()
                                .filter(sen::startsWith)
                                .min(Comparator.comparingInt(String::length))
                                .orElse(sen)
                ).collect(Collectors.joining(" "));
    }

    public static void main(String[] args) {
        List<String> dictionary = Arrays.asList("cat", "bat", "rat");
        String sentence = "the cattle was rattled by the battery";
        String s = replaceWords(dictionary, sentence);
        System.out.println("s = " + s);

    }
}
